/*
 * 浏览量自增长管理
 * @Author: along
 * @Date: 2019-12-17 11:39:00
 */
<template>
    <div class="container workBrowse">
        <!-- header -->
        <div class="headerBox">
            <el-tag type="warning">不建议设置太多，设定出上中下三挡即可</el-tag>
            <el-button
                type="primary"
                size="small"
                @click="fnCreateRule()"
            >添加浏览量增长套餐</el-button>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="workBrowse"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="inc_count_min"
                label="详情"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.inc_count_min ? scope.row.inc_count_min : 0 }}-{{ scope.row.inc_count_max ? scope.row.inc_count_max : 0 }}个/小时 （{{ scope.row.inc_count_min !== '' ? scope.row.inc_count_min * 24 : 0 }}-{{ scope.row.inc_count_max !== '' ? scope.row.inc_count_max * 24 : 0 }}个/天）</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEdit(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDelete(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 浏览量增长弹窗 -->
        <el-dialog
            title="浏览量增长规则"
            :visible.sync="dialogVisible"
            width="500px"
            :close-on-click-modal="false"
        >
            <div class="el-dialog-content">
                <el-input
                    v-model="minNum"
                    size="mini"
                    type="number"
                    style="width:120px"
                    min="0"
                    placeholder="请输入"
                />
                <span style="color:#606266"> &nbsp; - &nbsp; </span>
                <el-input
                    v-model="maxNum"
                    placeholder="请输入"
                    size="mini"
                    style="width:120px"
                    type="number"
                    min="0"
                />
                <span style="color:#606266">个/小时 （区间内随机增长）</span>
            </div>
            <p style="color:#606266;marginTop: 30px">预估：{{ minNum !== '' ? minNum * 24 : 0 }} &nbsp;-&nbsp;{{ maxNum !== '' ? maxNum * 24 : 0 }} 个/天</p>
            <span
                slot="footer"
                class="dialog-footer">
                <el-button
                    type="primary"
                    size="small"
                    @click="fnSave()"
                >保 存</el-button>
            </span>
        </el-dialog>
        <!-- /浏览量增长弹窗 -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->
    </div>
</template>

<script>
export default {
    /**
     * @param  {Object}    authResult                       //表单权限
     * @param  {Array}     tableData                        //表单数据
     * @param  {Object}    page                             //表单分页
     * @param  {Boolean}   dialogVisible                    //弹窗开关
     * @param  {Number}    maxNum                           //弹窗开关字段
     * @param  {Number}    minNum                           //弹窗开关字段
     * @param  {String}    type_popup                       //打开弹窗类型-新增/编辑
     * @param  {String}    autoinc_no                       //编号
     * @param  {String}    pathName                         //上一页路由name
     */
    data () {
        return {
            pathName: '',
            autoinc_no: '',
            type_popup: '',
            minNum: '',
            maxNum: '',
            dialogVisible: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    watch: {
        dialogVisible (val) {
            if (!val) {
                this.minNum = this.maxNum = '';
            }
        }
    },
    beforeRouteEnter(to, from, next) {
        next(vm => {
            vm.pathName = from.name;
        });
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnRecord();
        this.fnBreadcrumb();
        this.getTableData();

        // this.tableData = [{
        //     autoinc_no: 888,
        //     inc_count_min: 10,
        //     inc_count_max: 100
        // }];
    },
    destroyed(){
        window.removeEventListener('popstate', this.fnGoBack, false);
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '学习'},{title: "浏览量自增长管理"}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/course_autoinc%5Cget_list', {
                // page_id: page || this.page.now_page
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 记录上一页路由,监听页面返回并重定向
         */
        fnRecord () {
            if (window.history && window.history.pushState) {
                history.pushState(null, null, document.URL);
                window.addEventListener('popstate', this.fnGoBack, false);
            }
        },

        /**
         * 上一页重定向
         */
        fnGoBack  () {
            this.$router.replace({ name: this.pathName });
        },

        /**
         * 新建增长套餐
         */
        fnCreateRule () {
            this.type_popup = 'add';
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 编辑
         */
        fnEdit (row) {
            this.type_popup = 'edit';
            this.autoinc_no = row.autoinc_no;
            this.minNum = row.inc_count_min;
            this.maxNum = row.inc_count_max;
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 保存
         */
        fnSave () {
            if (this.minNum == '' || this.maxNum == '') {
                this.$message({
                    type: 'waring',
                    message: '增长区间不可以为空',
                    center: true
                });
                return;
            }

            if (Number(this.maxNum) == Number(this.minNum)) {
                this.$message({
                    type: 'waring',
                    message: '不可创建一模一样区间的规则',
                    center: true
                });
                return;
            }

            if (Number(this.maxNum) < Number(this.minNum)) {
                this.$message({
                    type: 'waring',
                    message: '区间规则有误,请重新填写',
                    center: true
                });
                return;
            }

            if (this.type_popup == 'add') {
                this.$post('/student/course_autoinc%5Cadd',{
                    inc_count_min: this.minNum,
                    inc_count_max: this.maxNum
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.dialogVisible = !this.dialogVisible;
                        this.$message({
                            type: 'success',
                            message: '添加成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            } else if (this.type_popup == 'edit') {
                this.$post('/student/course_autoinc%5Cset',{
                    autoinc_no: this.autoinc_no,
                    inc_count_min: this.minNum,
                    inc_count_max: this.maxNum
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.dialogVisible = !this.dialogVisible;
                        this.$message({
                            type: 'success',
                            message: '编辑成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            } else {
                return;
            }
        },

        /**
         * 删除
         */
        fnDelete (row) {
            this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/course_autoinc%5Cdelete',{
                    autoinc_no: row.autoinc_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .headerBox {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10px;
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-dialog-content {
        display: flex;
        height: 32px;
        align-items: center;
    }
</style>
<style lang="less">
    .workBrowse .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .workBrowse .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .workBrowse .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .workBrowse .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>